package com.ruix.app.mapper;

import com.ruix.app.model.Comment;
import com.ruix.app.model.Notice;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by hadexs on 2017/4/25.
 */

public class NoticeDetailMapper implements ApiMapper<Notice> {

    @Override
    public Notice transform(JSONObject response) {
        Notice notice = null;
        JSONObject data = response.optJSONObject("data");
        if (data != null) {
            JSONObject noticeObject = data.optJSONObject("notice");
            if (noticeObject != null) {
                notice = new Notice().parse(noticeObject);
            }
            JSONArray commentArray = data.optJSONArray("comment_list");

            if (commentArray != null) {
                ArrayList commens = new ArrayList();

                for (int i = 0; i < commentArray.length(); i++) {
                    Comment comment = new Comment().parse(commentArray.optJSONObject(i));
                    commens.add(comment);
                }
                if (notice != null) {
                    notice.setComments(commens);
                }
            }

        }
        return notice;
    }
}